﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KP_CommonLibrary.KP_DataAccess;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using KP_CommonLibrary.KP_ObjectReferences;
using System.Data.SqlClient;

namespace KEEPER_BRR_13dotNET.DataAccess
{
    public class DAL_KPGiayToKemTheo : KP_DALListBase
    {
        public DAL_KPGiayToKemTheo()
            : base("tblGiayToKemTheo")
        {
        }

        public override DataTable Select()
        {
            return SqlHelper.ExecuteDataTable(KP_OCnnSql.Cnn_String, "kp_GiayToKemTheoSelectAll", null);
        }
        /// <summary>
        /// Lấy danh sách các giấy tờ ngầm định cho loại hồ sơ
        /// </summary>
        /// <returns></returns>
        public DataTable SelectChoLoaiHoSo(string maLoaiHs)
        {
            DataTable tblData= SqlHelper.ExecuteDataTable(KP_OCnnSql.Cnn_String, "kp_GiayToKemTheoSelectByDoiTuong", new object[] { maLoaiHs, 1});
            tblData.TableName = "tblTPHoSo";
            return tblData;
        }
        /// <summary>
        /// Lấy danh sách các giấy tờ ngầm định cho loại hồ sơ
        /// </summary>
        /// <returns></returns>
        public DataTable SelectChoNganh(string maNganh)
        {
            DataTable tblData = SqlHelper.ExecuteDataTable(KP_OCnnSql.Cnn_String, "kp_GiayToKemTheoSelectByDoiTuong", new object[] { maNganh, 2 });
            tblData.TableName = "tblTPHoSo";
            return tblData;
        }
        public void Update(SqlTransaction tran, DataTable tblGiayTo)
        {
            DataView vDeleted = new DataView(tblGiayTo, null, null, DataViewRowState.Deleted);
            DataView vCurrent = new DataView(tblGiayTo, null, null, DataViewRowState.CurrentRows);
            foreach (DataRowView rowview in vDeleted)
            {
                DeleteRow(tran, rowview);
            }
            foreach (DataRowView row in vCurrent)
            {
                if (row.Row.RowState == DataRowState.Added)
                {
                    Insert(tran, row.Row);
                }
                if (row.Row.RowState == DataRowState.Modified)
                {
                    Update(tran, row.Row);
                }
            }

        }
        private void DeleteRow(SqlTransaction tran, DataRowView row)
        {
            try
            {
                row.Row.RejectChanges();
                base.Delete(tran, row.Row);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (row.Row.RowState != DataRowState.Deleted)
                    row.Row.Delete();
            }
        }
    }
}
